/*
* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

use std::path::Path;

use anyhow::Context;
use ohmw_codegen::msg_def::MsgDefMap;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let msgs_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../idl/msg");
    let msg_def_map = MsgDefMap::from_msgs_dir(&msgs_dir).await
        .context("failed to scan message definitions")?;
    for (pkg_name, msg_defs) in msg_def_map.map.iter() {
        for msg_name in msg_defs.keys() {
            println!("cargo:rerun-if-changed=../../idl/msg/{}/{}.msg", pkg_name, msg_name);
        }
    }

    let out_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("src").join("msgs");
    msg_def_map.gen_rust_mod(out_dir, "crate::msgs").await
        .context("failed to generate Rust message modules")?;

    let cxx_out_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("c++");
    msg_def_map.gen_cxx_project(cxx_out_dir).await
        .context("failed to generate C++ message modules")?;

    let napi_out_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("napi");
    msg_def_map.gen_napi_project(napi_out_dir).await
        .context("failed to generate NAPI message modules")?;

    Ok(())
}
